package com.ztgf.pay.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.CustomizeUtil;
import com.ztgf.common.utils.ExcelExport2;
import com.ztgf.pay.models.bo.*;
import com.ztgf.pay.models.dto.PayFundDetailsDTO;
import com.ztgf.pay.models.vo.PayBalanceExpotPcVO;
import com.ztgf.pay.models.vo.PayBalanceExpotVO;
import com.ztgf.pay.models.vo.PayUserBalanceVO;
import com.ztgf.pay.models.vo.PayZjBalanceLogVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.ztgf.pay.models.service.IPayUserService;
import com.ztgf.pay.models.dto.PayUserDTO;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 用户信息表控制器
 *
 * @author mxli
 * @Date 2020-07-30 12:13:56
 */

@RestController
@RequestMapping("/api/payUser")
@Slf4j
@Api(value = "PayUserApiController",tags = "用户信息表Api接口")
public class PayUserApiController {

    @DubboReference
    private IPayUserService payUserService;

    @PostMapping("/add")
    @ApiOperation(value = "开户",notes = "开户")
    public Result add(PayUserSaveBO saveBO){
        return payUserService.add(saveBO);
    }

    @PostMapping("/edit")
    @ApiOperation(value = "修改账户信息",notes = "修改账户信息")
    public Result edit(PayUserSaveBO saveBO){
        return payUserService.edit(saveBO);
    }

    @PostMapping("/del")
    @ApiOperation(value = "销户",notes = "销户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "平台id",required = true,paramType = "query", dataType = "Long")
            ,@ApiImplicitParam(name = "accountType",value = "账户类型( 1:客户资金账户 3:合作方收益账户 4:手续费账户)",required = true,paramType = "query", dataType = "Integer")
    })
    public Result del(Long userId, int accountType){
        return payUserService.del(userId, accountType);
    }


    @PostMapping("/getUserBalance")
    @ApiOperation(value = "获取用户余额",notes = "获取用户余额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "平台id",required = true,paramType = "query", dataType = "Long")
            ,@ApiImplicitParam(name = "accountType",value = "账户类型( 1:客户资金账户 3:合作方收益账户 4:手续费账户)",required = true,paramType = "query", dataType = "Integer")
    })
    public Result<PayUserBalanceVO> getUserBalance(Long userId, int accountType){
        return payUserService.getUserBalance(userId, accountType);
    }

    @PostMapping("/queryPageZjBalanceLog")
    @ApiOperation(value = "获取中金当天交易流水",notes = "获取中金当天交易流水")
    public Result<Page<PayZjBalanceLogVO>> queryPageZjBalanceLog(PayZjBalanceLogBO bo){
        return payUserService.queryPageZjBalanceLog(bo);
    }

    @PostMapping("/queryPageZjHistoryBalanceLog")
    @ApiOperation(value = "获取中金历史交易流水",notes = "获取中金历史交易流水")
    public Result<Page<PayFundDetailsDTO>> queryPageZjHistoryBalanceLog(PayZjBalanceLogBO bo){
        return payUserService.queryPageZjHistoryBalanceLog1(bo);
    }
    @GetMapping("/queryPageZjHistoryBalanceRu")
    @ApiOperation(value = "获取中金历史交易流水入库",notes = "获取中金历史交易流水入库")
    public Result queryPageZjHistoryBalance(){
         payUserService.fundDetailByHistory();
        return new Result();
    }

    @PostMapping("/addBankCard")
    @ApiOperation(value = "绑卡",notes = "绑卡")
    public Result addBankCard(PayUserBankcardSaveBO bo){
        return payUserService.addBankCard(bo);
    }

    @PostMapping("/editBankCard")
    @ApiOperation(value = "修改绑卡",notes = "修改绑卡")
    public Result editBankCard(PayUserBankcardSaveBO bo){
        return payUserService.editBankCard(bo);
    }

    @PostMapping("/delBankCard")
    @ApiOperation(value = "解绑",notes = "解绑")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "平台id",required = true,paramType = "query", dataType = "Long")
            ,@ApiImplicitParam(name = "accountType",value = "账户类型( 1:客户资金账户 3:合作方收益账户 4:手续费账户)",required = true,paramType = "query", dataType = "Integer")
            ,@ApiImplicitParam(name = "bankCard",value = "卡号",required = true,paramType = "query", dataType = "String")
    })
    public Result delBankCard(Long userId, Integer accountType,  String bankCard){
        return payUserService.delBankCard(userId, accountType,  bankCard);
    }


    @PostMapping("/saveActivationBankCard")
    @ApiOperation(value = "激活卡片",notes = "激活卡片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "平台id",required = true,paramType = "query", dataType = "Long")
            ,@ApiImplicitParam(name = "accountType",value = "账户类型( 1:客户资金账户 3:合作方收益账户 4:手续费账户)",required = true,paramType = "query", dataType = "Integer")
            ,@ApiImplicitParam(name = "id",value = "原申请交易的合作方交易流水号",required = true,paramType = "query", dataType = "Integer")
            ,@ApiImplicitParam(name = "money",value = "中金打款金额，单位分",required = true,paramType = "query", dataType = "Integer")
    })
    public Result saveActivationBankCard(Long userId, int accountType, Long id, BigDecimal money){
        return payUserService.saveActivationBankCard(userId, accountType, id, money);
    }
    @PostMapping("/batchExportBalanceListByUserId")
    @ApiOperation(value = "导出admin获取中金交易流水历史列表",notes = "导出admin获取中金交易流水历史列表")
    public void batchExportBalanceList(HttpServletResponse response, PayZjBalanceBO bo){
        //创建一个数组用于设置表头
        String[] attributeNames = new String[]{"序号","转账时间",  "业务类型", "转账金额",  "备注",  "交易对手"};
        List<PayBalanceExpotPcVO> list= payUserService.batchExportBalanceListByPc(bo);
        if (CustomizeUtil.listIsNull(list)){
            PayBalanceExpotPcVO vo = new PayBalanceExpotPcVO();
            BigDecimal price = new BigDecimal("0.00");
            vo.setMoeny(price.setScale(2, BigDecimal.ROUND_UP));
            list.add(vo);
        }else {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setNum(i+1);
            }
        }
        ExcelExport2.export(response, list, attributeNames, "账单");
    }
}
